package com.origin.ebuy.bargd.service.good.impl;

import java.sql.SQLException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.origin.ebuy.bargd.dao.good.IGoodDao;
import com.origin.ebuy.bargd.dao.good.impl.GoodDaoImpl;
import com.origin.ebuy.bargd.service.good.IgoodService;
import com.origin.ebuy.bargd.service.order.IOrderService;
import com.origin.ebuy.bargd.service.order.impl.OrderServiceImpl;

public class GoodServiceImpl implements IgoodService {
	
	IGoodDao dao = new GoodDaoImpl();
	IOrderService orderService = new OrderServiceImpl();
	
	/**
	 * 添加商品
	 * @throws SQLException 
	 */
	@Override
	public int addGood(JSONObject object) throws SQLException {
		return dao.addGood(object);
	}

	/**
	 * 查询所有的商品
	 */
	@Override
	public JSONArray queryGoods() throws SQLException {
		String[] cols = {"id_gd","name_gd","price_gd","number_gd","imge_gd","state"};
		return dao.queryGood(cols,null,null);
	}

	/**
	 * 根据商品的id查找商品
	 */
	@Override
	public JSONObject queryGood(String id) throws SQLException {
		String[] cols = {"id_gd","name_gd","price_gd","number_gd","imge_gd","state"};
		Object[] obj = {id};
		JSONArray array = dao.queryGood(cols, "id_gd=?",obj);
		if(array.isEmpty()){
			return new JSONObject();
		}else{
			return array.getJSONObject(0);
		}
	}
	
	

	@Override
	public int updateGood(String gdid, String name, String price,
			String numbers, String image,String state) throws SQLException {
		String[] cols = {"name_gd","price_gd","number_gd","imge_gd","state"};
		Object[] param = {name,price,numbers,image,gdid,state};
		return dao.updateGood(cols,"id_gd=?", param);
	}

	@Override
	public int deleteGood(String gdid) throws SQLException {
		return dao.deleteById(gdid);
	}

	/**
	 * 前台查询商品
	 */
	@Override
	public JSONArray fgqueryGoods() throws SQLException {
		String[] cols = {"id_gd","name_gd","price_gd","number_gd","imge_gd","state"};
		return dao.queryGood(cols,"state=1",null);
	}

	/**
	 * 购买商品
	 */
	@Override
	public int buyGood(JSONObject object) throws SQLException {
		//查询商品的剩余数量
		//更新商品剩余数量
		//生成订单
		//商品id
		String id = object.getString("id");
		//商品库存剩余的数量
		int number = queryGoodLeftNumbers(id);
		//购买的数量
		int bugNumber = object.getIntValue("number");
		number = number - bugNumber;
		//更新剩余数量
		updateGoodNumber(number, id);
		//生成订单
		orderService.insertOrder(id, 
				object.getString("price"), 
				object.getString("uid"),
				String.valueOf(bugNumber),
				object.getString("sf"),
				object.getString("city"),
				object.getString("dtail")
				);
		return 0;
	}

	@Override
	public int queryGoodLeftNumbers(String id) throws SQLException {
		String[] cols = {"number_gd"};
		JSONArray array = dao.queryGood(cols, "id_gd="+id, null);
		if(!array.isEmpty()){
			return array.getJSONObject(0).getIntValue("number_gd");
		}
		return 0;
	}

	@Override
	public int updateGoodNumber(int number,String id) throws SQLException {
		String[] cols = {"number_gd"};
		Object[] param = {number};
		return dao.updateGood(cols, "id_gd="+id, param);
	}
	
}
